package com.gxa.p2p.service.impl;

import com.gxa.p2p.mapper.RechargeMapper;
import com.gxa.p2p.pojo.AccountInfo;
import com.gxa.p2p.pojo.Recharge;
import com.gxa.p2p.pojo.UserInfo;
import com.gxa.p2p.service.RechargeService;
import com.gxa.p2p.util.MD5Creater;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: cxy
 * Date: 2020/08/10 10:16
 * Description:
 * Version: V1.0
 */
@Service("rechargeService")
@Transactional(readOnly = true, isolation = Isolation.DEFAULT, propagation = Propagation.SUPPORTS)
public class RechargeServiceImpl implements RechargeService{
    @Resource
    RechargeMapper rechargeMapper;
    @Override
    public Integer doRecharge(Recharge recharge) {
        UserInfo userInfo = rechargeMapper.selectByUserId(recharge.getUserId());
        AccountInfo accountInfo = rechargeMapper.getAccountInfo(recharge.getUserId());
        accountInfo.setUsableAmount(accountInfo.getUsableAmount().add(recharge.getRechargeAmount()));
        recharge.setArrivalAmount(recharge.getRechargeAmount().subtract(recharge.getRechargeAmount().multiply(new BigDecimal(0.001))));
        recharge.setPhoneNumber(userInfo.getPhoneNumber());
        recharge.setRealName(userInfo.getRealName());
        recharge.setStatus("充值成功");
        recharge.setServiceCharge(recharge.getRechargeAmount().multiply(new BigDecimal(0.001)));
        rechargeMapper.insertSelective(recharge);
        return rechargeMapper.updateAccountInfo(accountInfo);
    }

    /**
     * 充值密码验证
     * @param recharge 验证密码
     * @return   返回值用来判断是否成功
     */
    @Override
    public Integer verify(Recharge recharge) {
        AccountInfo accountInfo = rechargeMapper.getAccountInfo(recharge.getUserId());
        if ( MD5Creater.encode(recharge.getRecPassword()).equals(accountInfo.getTradePassword()))
        { return 0; }
        else
        { return 1; }
    }

    /**
     * 分页/模糊查询
     * @param map  必须limit page参数   其余参数用于模糊查询
     * @return  返回recharge对象
     */

    @Override
    public List<Recharge> selectLimitLike(Map<String, Object> map) {
        Integer page = (Integer) map.get("page");
        Integer limit = (Integer)map.get("limit");
        map.put("page",(page-1)*limit);
        if(map.get("id") != null){
            map.put("id","%"+map.get("id")+"%");
        } if(map.get("userId") != null){
            map.put("userId","%"+map.get("userId")+"%");
        } if (map.get("rechargeAmount") != null) {
            map.put("rechargeAmount","%"+map.get("rechargeAmount")+"%");
        }
     if (map.get("rechargeType") != null) {
        map.put("rechargeType","%"+map.get("rechargeType")+"%");
    }
         if (map.get("status") != null) {
            map.put("status","%"+map.get("status")+"%");
        }
         if (map.get("phoneNumber") != null) {
            map.put("phoneNumber","%"+map.get("phoneNumber")+"%");
        }
        List<Recharge> selectList = rechargeMapper.selectLimitLike(map);
        return selectList;
    }

    /**
     * 获取页面数据总量
     */

    @Override
    public Integer count(Map<String, Object> map) {
        Integer count = rechargeMapper.count(map);
        return count;
    }

    /**
     * 全查
     * @return 数据全返回
     */
    @Override
    public List<Recharge> selectAll() {

        return rechargeMapper.rechargeAll();
    }

    /**
     * 通过userId 查询  用于前台
     * @param userId  用户编号
     */
    @Override
    public List<Recharge> selectByUserId(Integer userId) {
        return rechargeMapper.rechargeByUserId(userId);
    }

}
